<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单控件-购物车3</title>
    <script src="lib/vue.js"></script>
</head>

<body>
    <!-- 增加购物数量的增减 -->
    <div id="box">
        <input type="checkbox" @change="handleChange" v-model="isAllChecked">全选全不选
        <ul>
            <li v-for="data in datalist" :key="data.id">
                <input type="checkbox" v-model="checkgroup" :value="data" @change="handleLiChange">
                {{data}}
                <!-- <button @click="data.number--">del</button> -->
                <button @click="handleDelClick(data)">del</button>
                {{data.number}}
                <button @click="data.number++">add</button>
            </li>
            {{checkgroup}}
            <p>总金额计算:{{getSum()}}</p>
            <p>计算属性-总金额计算:{{getSums}}</p>
        </ul>
    </div>

    <script>
        new Vue({
            el: "#box",
            data: {
                isAllChecked: false,
                checkgroup: [],
                datalist: [{
                        name: "商品1",
                        price: 10,
                        number: 1,
                        id: "1"
                    },
                    {
                        name: "商品2",
                        price: 20,
                        number: 2,
                        id: "2"
                    },
                    {
                        name: "商品3",
                        price: 30,
                        number: 3,
                        id: "3"
                    }
                ]
            },
            methods: {
                getSum() {
                    var sum = 0;
                    for (var i in this.checkgroup) {
                        sum += this.checkgroup[i].number * this.checkgroup[i].price
                    }
                    return sum
                },
                handleChange() {
                    console.log(this.isAllChecked);
                    if (this.isAllChecked) {
                        this.checkgroup = this.datalist
                    } else {
                        this.checkgroup = []
                    }
                },
                handleLiChange() {
                    if (this.checkgroup.length === this.datalist.length) {
                        this.isAllChecked = true
                    } else {
                        this.isAllChecked = false
                    }
                },
                handleDelClick(data) {
                    console.log(data);
                    var number = data.number--;
                    if (number === 1) {
                        data.number = 1
                    }
                }
            },
            computed: {
                getSums() {
                    var sum = 0;
                    for (var i in this.checkgroup) {
                        sum += this.checkgroup[i].number * this.checkgroup[i].price
                    }
                    return sum
                }
            }
        })
    </script>

</body>

</html>